package com.dean.utils;

import com.dean.entity.TableField;
import com.dean.entity.TableInfo;
import com.dean.exception.DeanRuntimeException;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName FreemarkerUtils
 * @Description 模板解析工具类
 * @Author Administrator
 * @Date 2021/10/2412:36
 */
public class FreemarkerUtils{
    private static final Logger logger = LoggerFactory.getLogger(FreemarkerUtils.class);

    public static final String TEMPLATE_PATH ="/dean/init/templates/run";

    private static Configuration configuration;

    /**
     * 加载模板解析配置
     */
    private static void configurationInstance() throws IOException {
        if(configuration == null ){
            // 创建configuration对象
            configuration = new Configuration(Configuration.getVersion());
            // 设置模板所在路径
            File file = new File(TEMPLATE_PATH);
            configuration.setDirectoryForTemplateLoading(file);
            //configuration.setClassForTemplateLoading(FreemarkerUtils.class,"/templates/run");
            configuration.setNumberFormat("#");
            // 设置模板文件使用字符集
            configuration.setDefaultEncoding("utf-8");
        }
    }

    /**
     * 解析模板
     * @param ftlName
     * @param filePath
     * @param dataModel
     */
    public static void parseTemplate(String ftlName, String filePath,String fileName, Map<String,Object> dataModel) {
        Writer writer = null;
        try {
            configurationInstance();
            // 加载模板
            Template template = configuration.getTemplate(ftlName);
            File ftlPath = new File(filePath);
            if(!ftlPath.exists()){
                ftlPath.mkdirs();
            }
            filePath=filePath+File.separator+fileName;
            writer = new FileWriter(new File(filePath));
            // 创建一个输出对象
            template.process(dataModel,writer);
            logger.info("文件生成结束,文件保存路径:{}",filePath);
        }catch (IOException | TemplateException e) {
            logger.error("文件解析异常{}",e.getMessage(),e);
            DeanRuntimeException.throwException(20004,"文件解析异常");
        }finally {
            if(writer != null){
                try {
                    writer.close();
                } catch (IOException e) {
                    logger.error("对象writer关闭异常,",e.getMessage(),e);
                    DeanRuntimeException.throwException(20004,"对象writer关闭异常");
                }
            }
        }
    }

    public static void main(String[] args) {
        Map<String,Object> dataModel = new HashMap<String,Object>(16);
        dataModel.put("packageName","com.test");
        TableInfo tableInfo = new TableInfo();
        tableInfo.setTableName("TABLE_TEST");
        tableInfo.setEntityName("tableTest");
        tableInfo.setTableComment("test create entity");
        tableInfo.setFirstUpperEntityName("TableTest");
        dataModel.put("tableInfo",tableInfo);

        List<TableField> tableFields = new ArrayList<>();
        TableField  tableField = new TableField();
        tableField.setColumnName("DATA_ID");
//        tableField.setFieldName("dataId");
//        tableField.setFirstUpperFieldName("DataId");
        tableField.setDataLength(100l);
        tableField.setDataScale(0);
        tableField.setDataType("varchar2");
        tableField.setFieldType("String");

        TableField  tableField2 = new TableField();
        tableField2.setColumnName("DATA_DATE");
//        tableField2.setFieldName("dataDate");
//        tableField2.setFirstUpperFieldName("DataDate");
        tableField2.setDataLength(100l);
        tableField2.setDataScale(0);
        tableField2.setFieldType("Integer");
        tableField2.setDataType("Integer");
        tableFields.add(tableField);
        tableFields.add(tableField2);
        logger.debug("{}",tableFields);
        dataModel.put("tableFields",tableFields);

        parseTemplate("Entity_gingkoo.java.ftl","/dean/test/com/test/","TableTest.java",dataModel);
    }
}
